angular.module('edit-address',[])

.controller 'EditAddressCtrl', ($scope,$http,$state,showToast,$stateParams,$ionicHistory,$ionicScrollDelegate,userData,$localStorage) ->
    $scope.backState = $stateParams.origin
    console.log $scope.backState
    $scope.addrData= $localStorage.get 'addressInfo'
    console.log $scope.addrData
    $scope.$storage = $localStorage
    console.log $scope.$storage.addrlist
    if $scope.addrData.id != '0'
        $scope.showDel = true
    else
        $scope.showDel = false
    $scope.goBack=()->
        $ionicHistory.goBack()
#        if $scope.backState == 'order-status'
#            $state.go 'tab.order-status-orders'
#        else if $scope.backState == 'address-list'
#            $state.go 'tab.address-list'
    $scope.delete=()->
        getData={
            action:'deleteaddress'
            userid:userData.getToken()
            addressid:$scope.addrData.id
        }
        $http.get '/seller',params:getData
        .success (resp) ->
            console.log resp
            if resp.status == '0'
                showToast '删除地址成功'
                $scope.different = []
                _.each $scope.$storage.addrlist,(addr)->
                    if addr.id == $scope.addrData.id
                        $scope.different.push addr
                $scope.$storage.addrlist = _.difference $scope.$storage.addrlist,$scope.different
                $state.go 'tab.address-list'
            else
                showToast resp.msg

    formvalid = ->
        if $scope.addrData.reciveuser != '' and $scope.addrData.phonenumber != '' and $scope.addrData.address != ''
            return true
        else
            if $scope.addrData.reciveuser == ''
                $scope.namerequire = true
            else if $scope.addrData.reciveuser != ''
                $scope.namerequire = false
            if $scope.addrData.phonenumber == ''
                $scope.phonerequire = true
            else if $scope.addrData.phonenumber != ''
                $scope.phonerequire =false
            if $scope.addrData.address == ''
                $scope.addressrequire = true
            else if $scope.addrData.address != ''
                $scope.addressrequire = false
            return false

    formValidation = ->
        $scope.nameError = false
        $scope.telError = false
        if $scope.addrData.reciveuser.match(/^(([\u4E00-\u9FA5]{2,7})|([a-zA-Z]{3,10}))$/) == null
            $scope.nameError = true
            return false
        else if $scope.addrData.phonenumber.match(/^0?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/) == null
            $scope.telError = true
            return false
        else
            return true
    $scope.save=()->
        if formvalid()
            if formValidation()
                postData={
                    action:'editshopaddress'
                    userid:userData.getToken()
                    addressid:$scope.addrData.id
                    reciveuser:$scope.addrData.reciveuser
                    phonenumber:$scope.addrData.phonenumber
                    address:$scope.addrData.address
                    remark:$scope.addrData.remark
                }
                $http.post '/utf-8data/',postData
                .success (resp) ->
                    console.log resp
                    if resp.status == '0'
                        showToast '保存地址成功'
                        if $scope.addrData.id == '0'
                            $scope.newAddress = [
                                {
                                    id:resp.addressid
                                    reciveuser:$scope.addrData.reciveuser
                                    phonenumber:$scope.addrData.phonenumber
                                    address:$scope.addrData.address
                                    remark:$scope.addrData.remark
                                }
                            ]
                            $scope.$storage.addrlist = _.union $scope.$storage.addrlist,$scope.newAddress
                        else
                            $scope.editAddress = {
                                id: $scope.addrData.id
                                reciveuser: $scope.addrData.reciveuser
                                phonenumber: $scope.addrData.phonenumber
                                address: $scope.addrData.address
                                remark: $scope.addrData.remark
                            }
                            _.each $scope.$storage.addrlist,(addr)->
                                if $scope.editAddress.id == addr.id
                                    $scope.$storage.addrlist = _.without $scope.$storage.addrlist,addr
                            $scope.$storage.addrlist.push $scope.editAddress
                        if $scope.backState == 'order-status'
                            $state.go 'tab.order-status-orders'
                        else if $scope.backState == 'address-list'
                            $state.go 'tab.address-list'
                    else
                        showToast resp.msg
    _.extend $scope,
        goChoice: ->
            if $scope.backState == 'order-status'
                $state.go 'tab.province-data-orders',{origin:$scope.backState}
            else
                $state.go 'tab.province-data-myself',{origin:$scope.backState}
        changeInfo: ->
            $localStorage.set 'addressInfo', $scope.addrData
    $scope.$on '$ionicView.enter', ->
